def expand_to_fixed_ratio(x1, y1, x2, y2, target_ratio, image_width=None, image_height=None):
    """
    将矩形坐标扩充到固定比例，保持中心点不变
    参数:
        x1, y1: 左上角坐标
        x2, y2: 右下角坐标
        target_ratio: 目标宽高比（width/height）
        image_width: 图像宽度（可选，用于边界检查）
        image_height: 图像高度（可选，用于边界检查）
    返回:
        扩充后的坐标 (new_x1, new_y1, new_x2, new_y2)
    """
    # 计算当前矩形的宽度、高度和中心点
    width = x2 - x1
    height = y2 - y1
    center_x = (x1 + x2) / 2
    center_y = (y1 + y2) / 2
    
    # 计算当前宽高比
    current_ratio = width / height
    
    # 判断是否需要调整宽度或高度
    if current_ratio < target_ratio:
        # 当前矩形比目标比例更"瘦"，需要加宽
        new_width = height * target_ratio
        new_height = height
    else:
        # 当前矩形比目标比例更"胖"，需要加高
        new_width = width
        new_height = width / target_ratio
    
    # 计算新的坐标
    new_x1 = center_x - new_width / 2
    new_y1 = center_y - new_height / 2
    new_x2 = center_x + new_width / 2
    new_y2 = center_y + new_height / 2
    
    # 如果提供了图像尺寸，进行边界检查
    if image_width is not None and image_height is not None:
        new_x1 = max(0, new_x1)
        new_y1 = max(0, new_y1)
        new_x2 = min(image_width, new_x2)
        new_y2 = min(image_height, new_y2)
    
    return int(new_x1), int(new_y1), int(new_x2), int(new_y2)

# 示例使用
x1, y1, x2, y2 = 100, 100, 200, 150  # 原始矩形（宽100，高50，比例2:1）
target_ratio = 2 / 3
image_width, image_height = 640, 480  # 图像尺寸

new_coords = expand_to_fixed_ratio(x1, y1, x2, y2, target_ratio, image_width, image_height)
print("扩充后的坐标:", new_coords)